-- enter in "strict" mode, that is to say, giving error when globals are read before having been wrote once before in the global scope
-- this mimic a kind of global variable declaration. This change the default behavior of lua that is to return nil but no error
-- when an uninitialized global variable is read

gloablMetatable = {}
setmetatable(_G, gloablMetatable)
gloablMetatable.__declared = {}

---

gloablMetatable.__newindex = function (t, n, v)

	if not gloablMetatable.__declared[n] then
		local w = debug.getinfo(2, "S").what
		if w ~= "main" and w ~= "C" then
			error("*** assign to undeclared variable '"..n.."'\n", 2)
		end
		gloablMetatable.__declared[n] = true
	end
	rawset(t, n, v)
end
  
---
  
gloablMetatable.__index = function (t, n)
	if not gloablMetatable.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
		error("*** variable '"..n.."' is not declared\n", 2)
	end
	return rawget(t, n)
end

---

function global(...)
   for _, v in ipairs{...} do mt.__declared[v] = true end
end